﻿function save(element_id, storage_id)
{
  var element = document.getElementById(element_id);
  localStorage[storage_id] = element.value;
}

var StatusCountDown = 0;
function setStatus(text)
{
  var status_container = document.getElementById("option_status_container");
  var status = document.getElementById("option_status");

  if(status.innerHTML.length > 0)
  {
    status.innerHTML += "<br />";
    StatusCountDown++;
  }

  status_container.style.display = '';
  status.innerHTML += '<span class="ui-icon ui-icon-info" style="float: left; margin-right: .3em;"></span>';
  status.innerHTML += text;

  setTimeout(
  function()
  {
    if(StatusCountDown > 0)
    {
      StatusCountDown--;
    }
    else
    {
      status_container.style.display = 'none';
      status.innerHTML = "";
    }
  },
  3500);

}

function setDefaultValue(storage_id, default_value)
{
  if (!localStorage[storage_id])
    localStorage[storage_id] = default_value;
}

function getValue(storage_id)
{
  return localStorage[storage_id];
}

function load(element_id, storage_id)
{
  var element = document.getElementById(element_id);
  element.value = getValue(storage_id);
}

function options_init()
{
  $("button").button();
  
  $('#option_tabs').tabs();
  
  $('#option_window').dialog({
    autoOpen: false,
    minHeight: 300,
    minWidth: 500
  });
  
  load_options();
  KwickPlus_AddOptionLink();
}

function options_open()
{  
  $('#option_window').dialog("open");
}

// Saves options to localStorage.
function save_options()
{
  save("tooltip_bg_color", "tooltip_bg_color");
  save("photozoom_tablocation", "photozoom_tablocation");
  save("photozoom_tabfocus", "photozoom_tabfocus");
  setStatus("Einstellungen gespeichert. Seite muss ggf. neu geladen werden.");
}

// Restores select box state to saved value from localStorage.
function load_options()
{
  setDefaultValue("tooltip_bg_color", "#FCF3E4");
  setDefaultValue("photozoom_tablocation", "1");
  setDefaultValue("photozoom_tabfocus", "0");

  load("tooltip_color_selector", "tooltip_bg_color");
  load("tooltip_bg_color", "tooltip_bg_color");
  document.body.style.backgroundColor = localStorage["tooltip_bg_color"];
  load("photozoom_tablocation", "photozoom_tablocation");
  load("photozoom_tabfocus", "photozoom_tabfocus");
}

function KwickPlus_AddOptionLink()
{
  var NaviBar = document.getElementById("topnav1");
  if(!NaviBar)
    return;

  var NaviBar_List = NaviBar.getElementsByTagName('ul')[0];
  if(!NaviBar_List)
    return;

  $(NaviBar_List).append(
  "<li> <a id='kwickplus_optionlink' href='#' onclick='options_open(); return false;' style='color: white;'>" +
  "KwickPlus! <br/>" +
  "Einstellungen" +
  "</a> </li>");
}